package com.pair.micro.fee.modules.charge.controller;


import com.pair.micro.common.operatelog.annotations.OperateLog;
import com.pair.micro.common.operatelog.enums.OperateTypeEnum;
import com.pair.micro.common.response.MicroResult;
import com.pair.micro.common.utils.PageResult;
import com.pair.micro.fee.modules.charge.convert.ChargeConfigConvert;
import com.pair.micro.fee.modules.charge.entity.ChargeConfigEntity;
import com.pair.micro.fee.modules.charge.query.ChargeConfigDataQuery;
import com.pair.micro.fee.modules.charge.query.ChargeConfigQuery;
import com.pair.micro.fee.modules.charge.service.ChargeConfigService;
import com.pair.micro.fee.modules.charge.vo.ChargeConfigVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("fx/config")
@Tag(name = "收费设置")
@AllArgsConstructor
public class ChargeConfigController {

    private final ChargeConfigService chargeConfigService;
    @GetMapping("page")
    @Operation(summary = "收费设置分页查询")
    @PreAuthorize("hasAuthority('fee:config:list')")
    public MicroResult<PageResult<ChargeConfigVO>> page(@ParameterObject @Valid ChargeConfigQuery chargeConfigQuery){
        PageResult<ChargeConfigVO> page = chargeConfigService.page(chargeConfigQuery);
        return MicroResult.ok(page);
    }


    @GetMapping(value = "list")
    @Operation(summary = "收费设置列表查询")
    public MicroResult<List<ChargeConfigVO>> gardenGridList(@ParameterObject @Valid ChargeConfigDataQuery chargeConfigDataQuery){
        ChargeConfigQuery chargeConfigQuery =new ChargeConfigQuery();
        chargeConfigQuery.setGardenId(chargeConfigDataQuery.getGardenId());
        chargeConfigQuery.setChargeType(chargeConfigDataQuery.getChargeType());
        List<ChargeConfigVO> list =  chargeConfigService.selectChargeConfigList(chargeConfigQuery);
        return MicroResult.ok(list);
    }


    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('fee:config:save')")
    public MicroResult<String> save(@RequestBody @Valid ChargeConfigVO vo) {
        chargeConfigService.save(vo);

        return MicroResult.ok();
    }
    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('fee:config:info')")
    public MicroResult<ChargeConfigVO> get(@PathVariable("id") String id) {
        ChargeConfigEntity entity = chargeConfigService.getById(id);
        return MicroResult.ok(ChargeConfigConvert.INSTANCE.convert(entity));
    }
    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('fee:config:update')")
    public MicroResult<String> update(@RequestBody @Valid ChargeConfigVO vo) {
        chargeConfigService.update(vo);

        return MicroResult.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('fee:config:delete')")
    public MicroResult<String> delete(@RequestBody List<Long> idList) {
        chargeConfigService.delete(idList);
        return MicroResult.ok();
    }

    @PostMapping("import")
    @Operation(summary = "导入用户")
    @OperateLog(type = OperateTypeEnum.IMPORT)
    @PreAuthorize("hasAuthority('fee:config:import')")
    public MicroResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return MicroResult.error("请选择需要上传的文件");
        }
//        chargeConfigService.importByExcel(file, passwordEncoder.encode("123456"));

        return MicroResult.ok();
    }

    @GetMapping("export")
    @Operation(summary = "导出用户")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('fee:config:export')")
    public void export() {
//        chargeConfigService.export();
    }

}

